package com.aifeng.hassan_food.login

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.aifeng.base.BaseActivity
import com.aifeng.common_ui.helper.isMobile
import com.aifeng.common_ui.helper.textString
import com.aifeng.hassan_food.BuildConfig
import com.aifeng.hassan_food.R
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.getExtra
import com.aifeng.utils.subscribeBy
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.LoginViewModel
import kotlinx.android.synthetic.main.activity_register_phone.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class RegisterPhoneActivity : BaseActivity() {

    private var mTel: String = ""
    private var mCode: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register_phone)
        initTitle(title = "手机号")
    }

    override fun getViewModel() = getViewModel<LoginViewModel>()

    @SuppressLint("SetTextI18n")
    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_yzm -> {
                when {
                    register_account.text.isBlank() -> {
                        register_account.requestFocus()
                        toast("请输入手机号")
                        return
                    }
                    !register_account.text.isMobile() -> {
                        register_account.requestFocus()
                        toast("请输入正确的手机号")
                        return
                    }
                }

                getViewModel().getCode(register_account.textString(), "1")
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = { inner ->
                            mTel = register_account.textString()
                            mCode = inner
                            @Suppress("ConstantConditionIf")
                            if (BuildConfig.LOG_DEBUG) {
                                register_yzm.setText(mCode)
                                register_yzm.setSelection(register_yzm.text.length)
                            }

                            getViewModel().countDown(count = 60)
                                .bindLifeCycle(getLifecycleOwner())
                                .subscribeBy(
                                    onNext = {
                                        bt_yzm.text = "${60 - it}s后重试"
                                        bt_yzm.isClickable = false
                                    },
                                    onComplete = {
                                        bt_yzm.text = "重新获取"
                                        bt_yzm.isClickable = true
                                    }
                                )
                        }
                    )
            }
            R.id.bt_next -> {
                when {
                    register_account.text.isBlank() -> {
                        register_account.requestFocus()
                        toast("请输入手机号")
                        return
                    }
                    register_yzm.text.isBlank() -> {
                        register_yzm.requestFocus()
                        toast("请输入验证码")
                        return
                    }
                    !register_account.text.isMobile() -> {
                        register_account.requestFocus()
                        toast("请输入正确的手机号")
                        return
                    }
                    mCode.isEmpty() -> {
                        toast("请点击获取验证码")
                        return
                    }
                    register_account.textString() != mTel -> {
                        toast("手机号码不匹配，请重新获取验证码")
                        return
                    }
                    register_yzm.textString() != mCode -> {
                        register_yzm.requestFocus()
                        register_yzm.setText("")
                        toast("请输入正确的验证码")
                        return
                    }
                }

                getViewModel().checkPhone(
                    phone = register_account.textString(),
                    code = register_yzm.textString()
                ).bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = {
                            startActivity<RegisterDoneActivity>(
                                "phone" to mTel,
                                "code" to intent.getExtra("code")
                            )
                        }
                    )
            }
        }
    }

}
